//
//  SearchResultView.m
//  Cat
//
//  Created by lushitong on 2018/12/23.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "SearchResultView.h"
#import "ShopCollectionCell.h"
#import "HttpHomeManager.h"
#import "SearchResultInfo.h"
#import "DetailViewController.h"
#import "FLLoadingView.h"
#import "SearchResultCell.h"

static NSString *const cellID = @"FLHomeCollectionViewCell";

@interface SearchResultView()
@property (nonatomic, assign) BOOL isFirstOpen;     //首次打开
@property (nonatomic, strong) NSString *keyWord;
@property (nonatomic, strong) NSMutableArray *itemArray;

@property (nonatomic, assign) BOOL isRequest;
@property (nonatomic, strong) FLLoadingView *loadingView; // 加载的loading

@end

@implementation SearchResultView


- (void)sendRequestWithCommed:(BOOL)needFreshLoading andKeyWord:(NSString *)keyWord {
	_keyWord = keyWord;
	self.isRequest = needFreshLoading;
	[self.loadingView showInView:self updateViewyPoint:0.0];
	self.pageIndex = 1;
	[self sendRequest];
}

- (instancetype)initWithFrame:(CGRect)frame layout:(UICollectionViewFlowLayout *)layout {
	if (self = [super initWithFrame:frame layout:layout]) {
		self.addRefresh = YES;
		self.needLoadMore = YES;
		self.emheaderHeight = - NavigationBarHeight;
		self.collectionView.backgroundColor = COLOR_WITH_HEX(0xFAFAFA);
		[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
			make.edges.equalTo(@0);
		}];
		self.defaultImgstyle = EQKDefaultImgstyleNoData;// 设置暂无勋章界面
		self.emTitle = @"找不到你要的结果";
		self.collectionView.showsVerticalScrollIndicator = NO;
		self.collectionView.showsHorizontalScrollIndicator = NO;
		[self.collectionView registerClass:[SearchResultCell class] forCellWithReuseIdentifier:NSStringFromClass([SearchResultCell class])];
		[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
		
		self.isFirstOpen = YES;
	}
	return self;
}

- (void)sendRequest {
	if (!self.isRequest) {return;}
	if (self.pageIndex == 1) {
		[self.itemArray removeAllObjects];
		[self.collectionView reloadData];
	}
	[HttpHomeManager searchWithKeyWord:self.keyWord andIndex:@(self.pageIndex) withSuccess:^(NSArray *itemArray) {
		[self.loadingView dismiss];
		[self.itemArray addObjectsFromArray:itemArray];
		dispatch_async(dispatch_get_main_queue(), ^{
			[self.collectionView reloadData];
		});
		[self finishLoadData:itemArray];
	} failure:^(NSInteger code, NSString * _Nonnull message) {
		[FLProgressHUD showError:message];
	}];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
	return UIEdgeInsetsMake(0, kQKFitModuleDecimal(10.0), 0, kQKFitModuleDecimal(10.0));
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
	SearchResultCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SearchResultCell class]) forIndexPath:indexPath];
	NSArray *array = self.itemArray;
	[cell setModel:[array safeObjectAtIndex:indexPath.row]];
	return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
	return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
	return self.itemArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
	CGFloat width = (ScreenWidth - kQKFitModuleDecimal(24 + 9)) / 2;
	return CGSizeMake(width,width + kQKFitModuleDecimal(94 + 30));
}

//UICollectionView被选中时调用的方法
-(void)collectionView:( UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
	[collectionView deselectItemAtIndexPath:indexPath animated:YES];
	NSLog(@"%ld",(long)indexPath.row);
	NSArray *array = self.itemArray;
	GroomsModel *mode = [array safeObjectAtIndex:indexPath.row];
	DetailViewController *de = [[DetailViewController alloc] init];
	de.proudctID = mode.productId;
	[self.currentNavigationController pushViewController:de animated:YES];
}

- (NSMutableArray *)itemArray {
	if (!_itemArray) {
		_itemArray = [[NSMutableArray alloc] init];
	}
	return _itemArray;
}
- (FLLoadingView *)loadingView {
	if (!_loadingView) {
		_loadingView = [[FLLoadingView alloc] init];
		_loadingView.backgroundColor = [UIColor whiteColor];
	}
	return _loadingView;
}

@end
